'''
Created on Jan 27, 2013

@author: kafkef
'''

from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, Qt

from dexen.system import client
from dexen.system.client.ui.remote.table import RemoteTable


svr_proxy = client.GetServerProxy()


class NodesInfoWdg(QWidget):
    def __init__(self):
        super(NodesInfoWdg, self).__init__()
        self._create_widgets()

    def _create_widgets(self):
        self.refreshBtn = QPushButton("Refresh")
        self.refreshBtn.setEnabled(False)
        self.nodesTable = RemoteTable("NodesInfoWdg")
        self.nodesTable.setColumnCount(3)
        self.nodesTable.setHorizontalHeaderLabels(["Node Name", "Ip Address", "Port"])
        hLayout = QHBoxLayout()
        hLayout.addStretch()
        hLayout.addWidget(self.refreshBtn)
        
        layout = QVBoxLayout()
        layout.addLayout(hLayout)
        layout.addWidget(self.nodesTable)
        
        self.setLayout(layout)
        
        self.connect(self.refreshBtn, SIGNAL("clicked()"), self.get_node_states)
    
    def get_node_states(self):
        node_states = svr_proxy.get_node_states()
        self.nodesTable.setRowCount(len(node_states))
        self.nodesTable.setSortingEnabled(False)    
        for row, node_state in enumerate(node_states):
            self.nodesTable.setItem(row, 0, QTableWidgetItem(node_state.name))
            self.nodesTable.setItem(row, 1, QTableWidgetItem(node_state.ip))
            self.nodesTable.setItem(row, 2, QTableWidgetItem(str(node_state.port)))
        self.nodesTable.setSortingEnabled(True)
    
    def on_connect(self):
        self.refreshBtn.setEnabled(True)

    def on_disconnect(self):
        self.refreshBtn.setEnabled(False)
        self.nodesTable.setRowCount(0)
    
    def save_settings(self):
        self.nodesTable.save_settings()
    
    def restore_settings(self):
        self.nodesTable.restore_settings()
    
